//
//  MCCheckOrderViewController.swift
//  StoreManage
//
//  Created by MC on 2018/6/20.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCCheckOrderViewController: MCViewController {

    private var pageNumber = 1

    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        managerClosure()
        
        sendNetworking()
    }
    
    // MARK: - Setter & Getter
    lazy private var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()

    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    
    lazy var headView: MCCheckOrderTopView = {
        let view = MCCheckOrderTopView()
        return view
    }()
}

//MARK: 通知回调，闭包回调，点击事件
extension MCCheckOrderViewController {
    func managerClosure() {
        headView.closure = { vc in
            MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
        }
    }

}


//MARK: 网络请求
extension MCCheckOrderViewController {
    func sendNetworking() {
        let para = [
            "pageNum"   : pageNumber,
            "pageSize"  : 10
            ] as [String : Any]
        
        MCNetwork.POST(JAVA_getWriteOffRecord, para, vc: self, success: { (data) in
            
            let dataArray = data.arrayObject ?? []
            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
            
            if self.pageNumber == 1 {
                self.dataArrayM.removeAllObjects()
                self.tableView.mj_footer.resetNoMoreData()
            }
            
            if dataArray.count == 0 {
                self.tableView.mj_footer.endRefreshingWithNoMoreData()
            }
            
            if dataArray.count + self.dataArrayM.count == 0 {
                self.tableView.showBgImage()
            } else {
                self.tableView.hideBgImage()
                for dict in dataArray {
                    let model = MCCheckOrderModel()
                    model.setValuesForKeys(dict as? [String:Any] ?? [:])
                    self.dataArrayM.add(model)
                }
                self.tableView.reloadData()
            }

        }, failure: nil)
    }

}

//MARK: UI的处理,通知的接收
extension MCCheckOrderViewController {
    func baseSetting() {
        self.title = "核销"
        self.view.backgroundColor = UIColor.white
        
        self.tableView.mj_header = MCRefreshHeader.init(refreshingBlock: {
            self.pageNumber = 1
            self.sendNetworking()
        })
        
        self.tableView.mj_footer = MCRefreshFooter.init(refreshingBlock: {
            self.pageNumber += 1
            self.sendNetworking()
        })
        
    }
    
    func initUI() {
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view).inset(UIEdgeInsetsMake(0, 0, MCSafeAreaBottomHeight, 0))
        }
        
        tableView.tableHeaderView = headView
    }

}


extension MCCheckOrderViewController : UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArrayM.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCCheckOrderCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.backgroundColor = UIColor.MCGray_light
        cell.model = self.dataArrayM[indexPath.row] as! MCCheckOrderModel
        
        
        return cell
    }
}


